package com.service.impl;

import com.mapper.EmpAndLeaveApplyMapper;
import com.mapper.EmployeeMapper;
import com.mapper.LeaveApplyMapper;
import com.pojo.Employee;
import com.pojo.view.EmpAndLeaveApply;
import com.pojo.LeaveApply;
import com.service.LeaveApplyService;
import com.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.ArrayList;
import java.util.List;

public class LeaveApplyServiceImpl implements LeaveApplyService {

    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();


    /**
     * 将一条leaveApply记录添加至数据库表tb_leaveapply
     * @param leaveApply
     */
    @Override
    public void addToLeaveApply(LeaveApply leaveApply) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        LeaveApplyMapper mapper = sqlSession.getMapper(LeaveApplyMapper.class);

        mapper.add(leaveApply);
        sqlSession.commit();

        sqlSession.close();
    }

    /**
     * 将员工对应的leaveApply记录添加数据库表tb_emp_leaveapply
     * @param empAndLeaveApply
     */
    @Override
    public void addToEmpLeaveApply(EmpAndLeaveApply empAndLeaveApply) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpAndLeaveApplyMapper mapper = sqlSession.getMapper(EmpAndLeaveApplyMapper.class);

        mapper.addItem(empAndLeaveApply);
        sqlSession.commit();

        sqlSession.close();
    }

    /**
     * 根据applytime查找id
     * @param applyTime
     * @return
     */
    @Override
    public int getLeaveApplyIdByApplyTime(String applyTime) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        LeaveApplyMapper mapper = sqlSession.getMapper(LeaveApplyMapper.class);
        LeaveApply leaveApply = mapper.getByApplyTime(applyTime);
        int leaveApplyId = leaveApply.getId();

        sqlSession.close();
        return leaveApplyId;
    }

    /**
     * 查找status为“正在审核”的leaveapply集合
     * @return
     */
    @Override
    public List<LeaveApply> getLeaveApplyChecks() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        LeaveApplyMapper mapper = sqlSession.getMapper(LeaveApplyMapper.class);
        List<LeaveApply> leaveApplyList = mapper.getByStatus("正在审核");

        sqlSession.close();
        return leaveApplyList;
    }

    /**
     * 根据leaveapply_id查找对应的员工
     * @return
     */
    @Override
    public Employee getEmployeeByLeaveApplyId(int leaveApplyId) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpAndLeaveApplyMapper empAndLeaveApplyMapper = sqlSession.getMapper(EmpAndLeaveApplyMapper.class);
        EmpAndLeaveApply empAndLeaveApply = empAndLeaveApplyMapper.getByLeaveApplyId(leaveApplyId);
        String username = empAndLeaveApply.getUsername();

        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = employeeMapper.getByUsername(username);
        return employee;
    }

    /**
     * 根据leaveapply_id修改相应申请的status为“已通过”
     * @param leaveapply_id
     */
    @Override
    public void updateStatusToApprovedById(int leaveapply_id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        LeaveApplyMapper mapper = sqlSession.getMapper(LeaveApplyMapper.class);
        mapper.updateStatusToApprovedById(leaveapply_id);

        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 根据leaveapply_id查找对应的请假申请
     * @param leaveApplyId
     * @return
     */
    @Override
    public LeaveApply getLeaveApplyByLeaveApplyId(int leaveApplyId) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        LeaveApplyMapper mapper = sqlSession.getMapper(LeaveApplyMapper.class);
        LeaveApply leaveApply = mapper.getById(leaveApplyId);

        sqlSession.close();
        return leaveApply;
    }

    /**
     * 根据username查找对应的请假申请Id
     * @param username
     * @return
     */
    @Override
    public List<Integer> getLeaveApplyIdListByUsername(String username) {
        List<Integer> leaveApplyIdList = new ArrayList<>();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpAndLeaveApplyMapper mapper = sqlSession.getMapper(EmpAndLeaveApplyMapper.class);
        List<EmpAndLeaveApply> empAndLeaveApplyList = mapper.getAllByUsername(username);
        for (EmpAndLeaveApply empAndLeaveApply : empAndLeaveApplyList) {
            leaveApplyIdList.add(empAndLeaveApply.getLeaveApplyId());
        }

        sqlSession.close();
        return leaveApplyIdList;
    }

    /**
     * 查找"正在审核"的申请数目
     * @return
     */
    @Override
    public Integer getWaitToCheckOfAll() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        LeaveApplyMapper mapper = sqlSession.getMapper(LeaveApplyMapper.class);
        List<LeaveApply> leaveApplyList = mapper.getWaitToCheck();
        Integer waitToCheck = leaveApplyList.size();

        sqlSession.close();
        return waitToCheck;
    }

    /**
     * 根据leaveapply_id修改相应申请的status为“未通过”
     *
     * @param leaveApplyId
     */
    @Override
    public void updateStatusToRefusedById(int leaveApplyId) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        LeaveApplyMapper mapper = sqlSession.getMapper(LeaveApplyMapper.class);
        mapper.updateStatusToRefusedById(leaveApplyId);

        sqlSession.commit();
        sqlSession.close();
    }
}
